/*
 * MIT License
 *
 * Copyright (c) 2021 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.seaboot.excel;

import cn.seaboot.commons.file.IOUtils;
import cn.seaboot.excel.call.ExcelCallback;
import cn.seaboot.excel.call2.StringArrayReaderAndWriter;
import cn.seaboot.excel.header.Header;
import cn.seaboot.excel.style.SimpleStyleProvider;
import cn.seaboot.excel.tools.CommentTools;
import cn.seaboot.excel.tools.WatermarkTools;
import org.apache.poi.ss.usermodel.Workbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * 测试函数
 *
 * @author Mr.css
 * @version 2021-02-01 11:58
 */
public class Example {

    private static final String file = "C:\\Users\\postm\\Desktop\\output.xlsx";

    /**
     * 水印
     *
     * @param args -
     * @throws IOException -
     */
    public static void watermark(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File("C:\\Users\\ASUS\\Desktop\\1740022283171.png"));
        try (OutputStream os = Files.newOutputStream(Paths.get(file));
             ExcelBook book = ExcelBook.create()) {
            WatermarkTools tools = new WatermarkTools(book);
            tools.watermark(image, "png");
            book.out(os);
        }
    }

    /**
     * 读取大型文档
     *
     * @param args -
     * @throws IOException -
     */
    public static void readLargeExcel(String[] args) throws IOException {
        // 简单的数据读取
        try (InputStream is = Files.newInputStream(Paths.get(file))) {
            List<Object[]> list = LargerExcelReader
                    .create(is)
                    .sheetAt(0)
                    .setRowReader(new StringArrayReaderAndWriter())
                    .read();

            for (Object[] row : list) {
                System.out.println(Arrays.toString(row));
            }
        }
    }


    /**
     * 单元格批注
     *
     * @param args -
     * @throws IOException -
     */
    public static void style(String[] args) throws IOException {
        Header[] headers = new Header[]{
                new Header("a", "a", "adadssa", null),
                new Header("b", "b", "adadssa", null)
        };

        // 写入数据的时候，切换样式
        try (OutputStream os = Files.newOutputStream(Paths.get(file));
             ExcelBook book = ExcelBook.create()) {
            book.setRowReaderAndWriter(ExcelCallback.objectArrayCallback())
                    .sheetAt(0)
                    .setStyleProvider(new SimpleStyleProvider())
                    .setStyle("title").writeTitle(0, 2, "标题")
                    .setStyle("header").writeHeader(1, headers)
                    .out(os);
        }
    }

    /**
     * 单元格批注
     *
     * @param args -
     * @throws IOException -
     */
    public static void comment(String[] args) throws IOException {
        try (OutputStream os = Files.newOutputStream(Paths.get(file));
             ExcelBook book = ExcelBook.create()) {
            book.setRowReaderAndWriter(ExcelCallback.objectArrayCallback())
                    .sheetAt(0)
                    .setStyleProvider(new SimpleStyleProvider())
                    .setStyle("title").writeTitle(0, 2, "标题")
                    .setStyle("header").writeArray(1, new String[]{"a", "b"});

            // 增加单元格批注
            CommentTools tools = new CommentTools(book);
            tools.comment(1, 0, "adasda");
            tools.comment(1, 1, "adasda");

            book.out(os);
        }
    }


    /**
     * 一个简单的案例
     *
     * @param args -
     * @throws IOException -
     */
    public static void example(String[] args) throws IOException {
        //使用模版的写法，使用无参的create()函数，则可以凭空创建Excel
        try (InputStream is = Files.newInputStream(Paths.get(file));
             ExcelBook book = ExcelBook.create(is)) {

            //设置工作表
            book.sheetAt(0);
            //起始行号
            book.setStartRow(0);
            //结束行号，如果不设置，取数时会读到最后一行
            book.setLastRowAsEndRow();
            //写入数据时，会复制Excel第0行，第0个单元格样式，也允许自己设置Style对象
            book.cloneStyle(0, 0);
            //读写回调，ExcelCallback中提供了常规对象的读写方式
            book.setRowReaderAndWriter(ExcelCallback.objectArrayCallback());

            //读取0-100行的数据
            List<Object[]> res = book.read();

            for (Object[] arr : res) {
                System.out.println(Arrays.toString(arr));
            }

            // 切换到 sheet1
            book.createSheetAt(1);
            // 设置列宽
            book.setColumnWidth(0, 20 * 256);
            book.setColumnWidth(1, 30 * 256);
            // 将内容写入到工作簿 1
            book.write(res);

            // 文件另存
            try (OutputStream os = IOUtils.openFileOutputStream("C:\\Users\\ASUS\\Desktop\\a.xlsx")) {
                book.out(os);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        readLargeExcel(args);
    }
}
